//
//  PercentCurrentPiePlot.m
//  Balance
//
//  Created by oracle on 7/20/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PercentCurrentPiePlot.h"

@implementation PercentCurrentPiePlot


@synthesize graph,pieData,item;


- (id) initWithMetricsData:(MetricsItem *) data {
    
    item=data;  
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyy/MM/dd"];  
    titles=[[NSArray alloc] initWithObjects:@"Percent Current ",
            @"Percent 1-30 Days Delinquent ",
            @"Percent 31-60 Days Delinquent ",
            @"Percent 61-90 Days Depliquent ",
            @"Percent 91+ Days Delinquent ",nil
            ];
    
    return self;
    
}

- (void)respondToValueChange: (PercentCurrentLinePlot *)plot item:( MetricsItem *) metricsItem{
    
    item=metricsItem;
    graph.title = [@"Percent Delinquent As of "stringByAppendingString: [dateFormatter stringFromDate:item.itemDate] ];
    self.pieData=  [NSMutableArray arrayWithObjects:
                    item.percentCurrent, 
                    item.percent1To30DaysDelinquent,
                    item.percent31To60DaysDelinquent,
                    item.percent61To90DaysDelinquent,
                    item.percent90OverDaysDelinquent, 
                    nil];
    [graph reloadData];
}

- (void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)mytheme{
  
    graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
    
    layerHostingView.hostedGraph = graph;
    
    layerHostingView.backgroundColor = [UIColor colorWithWhite:0.7f alpha:1.0f]; 
    
    CPTGradient *plotAreaGradient = [CPTGradient gradientWithBeginningColor:
                                    [CPTColor colorWithGenericGray:0.9f]
                                    endingColor:[CPTColor colorWithGenericGray:0.7f]];
    plotAreaGradient.angle = -90.0;
    graph.plotAreaFrame.plotArea.fill = [CPTFill fillWithGradient:plotAreaGradient]; 
    
       
    graph.plotAreaFrame.masksToBorder = NO;
    
    CPTMutableTextStyle *blackTextStyle = [[CPTMutableTextStyle alloc] init];
   
    blackTextStyle.color = [CPTColor colorWithComponentRed:0.2 green:0.2 blue:0.2 alpha:0.82]; //RGB:R51-G51-B51
	blackTextStyle.fontSize = 20.0;
    graph.titleTextStyle = blackTextStyle;
    graph.titleDisplacement = CGPointMake(0.0, 20.0); 
    
   
    graph.title = [@"Percent Delinquent As of "stringByAppendingString: [dateFormatter stringFromDate:item.itemDate] ];
    
    // Add plot space for horizontal bar charts
    graph.paddingLeft = 90.0;
	graph.paddingTop = 40.0;
	graph.paddingRight = 70.0;
	graph.paddingBottom = 1.0; 
    graph.axisSet=nil;
        
    // Add pie chart
    CPTPieChart *piePlot = [[CPTPieChart alloc] init];    
        
	piePlot.dataSource = self;
	piePlot.pieRadius = 65.0;
        
	piePlot.identifier = @"PercentCurrentPieChart";
	piePlot.startAngle = M_PI_4;
	piePlot.sliceDirection = CPTPieDirectionCounterClockwise;
    
    
    //Legend
    /*
    CPTLegend *theLegend = [CPTLegend legendWithGraph:graph];
    theLegend.numberOfColumns = 5;
    theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
    theLegend.borderLineStyle = [CPTLineStyle lineStyle];
    theLegend.cornerRadius = 5.0;
    
    graph.legend = theLegend;
     
    
    graph.legendAnchor = CPTRectAnchorRight;
    graph.legendDisplacement = CGPointMake(0.0, 30.0);
    */
    
	self.pieData=  [NSMutableArray arrayWithObjects:
                   item.percentCurrent, 
                   item.percent1To30DaysDelinquent,
                   item.percent31To60DaysDelinquent,
                   item.percent61To90DaysDelinquent,
                   item.percent90OverDaysDelinquent, nil];
     piePlot.delegate = self;    
    [graph addPlot:piePlot];
    
}

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
   static CPTMutableTextStyle *textStyle = nil;

   if ( !textStyle ) {
       textStyle = [[CPTMutableTextStyle alloc] init];
       textStyle.color = [CPTColor blackColor];
       textStyle.fontSize=10;
    }
    NSString *title= [NSString stringWithFormat:@"%2.2f", 
                   [[pieData objectAtIndex:index] floatValue]]; 
    
  
       
    title=[[titles objectAtIndex: index] stringByAppendingString:title ]; 
    
    title=[title stringByAppendingString: @"%"];
   // NSLog(@"index=%i count=%i title=%@",index,[titles count],title);
    
    CPTTextLayer *newLayer = [[CPTTextLayer alloc] initWithText: title style:textStyle] ;
    
    title=@"";
    
    return newLayer;
}


-(void)pieChart:(CPTPieChart *)plot sliceWasSelectedAtRecordIndex:(NSUInteger)index
{
    NSLog(@"Slice was selected at index %d. Value = %f", (int)index, [[pieData objectAtIndex:index] floatValue]);
}

-(NSString *)legendTitleForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index {
    if(index==1){
        return @"Percent Current";
    } 
     return [NSString stringWithFormat:@"Percent Current %u", index];    
}

-(CGFloat)radialOffsetForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index
{
    return ( index == 0 ? 0.0f : 0.0f );
}

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
    return [self.pieData count];
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{
    if ( index >= [self.pieData count] ) return nil;
    
    if ( fieldEnum == CPTPieChartFieldSliceWidth ) {
        return [self.pieData objectAtIndex:index];
    }
    else {
        return [NSNumber numberWithInt:index];
    }	
}

- (void)viewDidUnload  {
	[self setPieData:nil];
	[graph setGraph:nil];
	 dateFormatter =nil;
}


@end
